package com.iot.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iot.domain.Light;
import com.iot.domain.Spl;
import com.iot.service.LightService;
import com.iot.service.MessageService;
import com.iot.service.SplService;
import com.iot.util.AliIOTMessage;
import com.iot.util.DateFormat;
import com.iot.util.GetRequestJsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wlr
 * @since 2022-01-06
 */
@RestController
public class MessageController {
    @Autowired
    MessageService messageService;

    @Autowired
    SplService splService;

    @Autowired
    LightService lightService;

    DateFormat dateFormat = new DateFormat();

    @PostMapping(value = "/message")
    public String handleAmqp(HttpServletRequest request) {
        try {
            String str = GetRequestJsonUtils.getRequestJsonString(request);
            AliIOTMessage aliIOTMessage = JSONArray.parseObject(str, AliIOTMessage.class);
            handle(aliIOTMessage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }


    public String[] splitJson(String str){
        str=str.replaceAll("\\{", "").replaceAll("\\}", "").replaceAll("\"", "").replaceAll("time:","").replaceAll("value:","");
        return str.split(",");
    }
    public void handle(AliIOTMessage aliIOTMessage) {
        if (aliIOTMessage.getDeviceName() != null) {
            LocalDateTime localDateTime = dateFormat.strToLocalDateTime(aliIOTMessage.getGmtCreate());
            com.iot.domain.Message message = new com.iot.domain.Message(aliIOTMessage.getDeviceType(), aliIOTMessage.getIotId(), aliIOTMessage.getCheckFailedData(), aliIOTMessage.getProductKey(), localDateTime, aliIOTMessage.getDeviceName());
            messageService.insert(message);

            Map<String, String> items = aliIOTMessage.getItems();
            for (String s : items.keySet()) {
                if (s.equals("light")) {
                    String[] str = splitJson(items.get(s));
                    String time = str[0];
                    Double value = Double.valueOf(str[1]);
                    LocalDateTime time1 = dateFormat.strToLocalDateTime(time);
                    Light light = new Light(aliIOTMessage.getDeviceName(), value, time1);
                    lightService.insert(light);
                } else if (s.equals("spl")) {
                    String[] str = splitJson(items.get(s));
                    String time = str[0];
                    Double value = Double.valueOf(str[1]);
                    LocalDateTime time1 = dateFormat.strToLocalDateTime(time);
                    Spl spl = new Spl(aliIOTMessage.getDeviceName(), value, time1);
                    splService.insert(spl);
                }
            }
        } else {
            return;
        }

    }
}

